/*
 * Copyright (C) 2013 AndXmppSDK Project <hljdrl@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.drl.xmpp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.packet.Message;

import android.content.Context;

/**
 * 
 * 聊天管理器
 * 
 * @author hljdrl@gmail.com
 */
public final class TChatManager implements ChatManagerListener, MessageListener {

	private final ChatManager mChatManager;
	private Context mContext;
	private RosterManager mRoster;
	//
	private final Map<String, TChat> mChats = new HashMap<String, TChat>();
	private final List<ChatManagerListener> mRemoteChatCreationListeners = new ArrayList<ChatManagerListener>();
	private TalkClient mTalk;;

	public TChatManager(TalkClient talk,ChatManager chatManager) {
		
		mChatManager = chatManager;
		mRoster = talk.getRosterManager();
//		mRoster.addRosterListener(this);
		mTalk = talk;
		mChatManager.addChatListener(this);
	}

	public TChat createChat(Contact contact, MessageListener listener) {
		return createTChat(contact, listener);
	}
	TChat createTChat(Contact contact,MessageListener listener){
		String key = contact.getJIDWithRes();
		TChat result ;
		if(mChats.containsKey(key)){
			result = mChats.get(key);
			result.addMessageListener(listener);
			return result;
		}
		Chat c = mChatManager.createChat(key, null);
		result = getChat(c);
		result.addMessageListener(listener);
		return result;
	}

	private TChat getChat(Chat chat) {
		String key = chat.getParticipant();
		if(mChats.containsKey(key)){
			return mChats.get(key);
		}
		TChat tchat = new TChat(chat, mTalk);
		mChats.put(key, tchat);
		return tchat;
	}
	public TChat getChat(Contact contact)  {
		String key = contact.getJIDWithRes();
		return mChats.get(key);
	}


	public void destroyChat(TChat chat) {
		if(chat==null){
			return;
		}
		mChats.remove(chat.getParticipant().getJID());
	}

	void deleteChatNotification(TChat chat) {
	}

	void addChatCreationListener(ChatManagerListener listener) {
	}

	void removeChatCreationListener(ChatManagerListener listener) {
	}

	List<Contact> getOpenedChatList() {
		return null;
	}

	TMessage getNewNoReadMessage() {
		return null;
	}

	@Override
	public void chatCreated(Chat chat, boolean arg1) {
			TChat newchat = getChat(chat);
			newchat.addMessageListener(this);
	}

	@Override
	public void processMessage(Chat chat, Message smackmsg) {
		TMessage msg = new TMessage(smackmsg);
		
		
	}

}
